package com.cg.service.impl;

import com.cg.dao.PatientMapper;
import com.cg.dao.PatientUserMapper;
import com.cg.dto.Result;
import com.cg.dto.ResultUtils;
import com.cg.entity.Patient;
import com.cg.service.NumService;
import com.cg.service.PatientService;
import com.cg.service.PatientUserTokenService;
import com.cg.util.IdUtils;
import com.cg.utils.NumUtils;
import com.cg.vo.PatientListVo;
import com.cg.vo.PatientVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Patient)表服务实现类
 *
 * @author chengong
 * @since 2023-12-31 13:47:36
 */
@Service("patientService")
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientMapper patientMapper;

    @Resource
    private PatientUserTokenService patientUserTokenService;

    @Resource
    private NumService numService;

    @Resource
    private PatientUserMapper patientUserMapper;

    @Value("${patientUserTokenTimeOut}")
    private Long patientUserTokenTimeOut;


    @Resource
    private PatientUserServiceImpl patientUserService;
    //输出路径
    @Value("${pathStr}")
    private String pathStr;

    @Resource
    private IdUtils idUtils;
    /**
     * 就诊人管理列表
     */
    @Override
    public List<PatientListVo> patientListByPhone(Integer patientUserId) {
        return patientMapper.patientListByPhone(patientUserId);
    }

    /**
     * 查询就诊人详情
     */
    @Override
    public PatientVo queryPatientByCardNum(String cardNum) {
        return patientMapper.queryPatientByCardNum(cardNum);
    }

    /**
     * 添加就诊人
     */
    @Override
    public Integer createPatient(Patient patient) {
        Integer count = 0;
        Boolean flag = this.checkIdCard(patient.getIdCard());
        if(!flag){
            count = 0;
        }

        //生成CardNum就诊卡号
        String carNum = idUtils.getCarNum();
        //用户输入的身份证号
        String idCard = patient.getIdCard();
        //通过身份证号获取出生年月
        String birthday = NumUtils.getBirthdayByIdCard(idCard);
        //通过身份证号获取性别
        Integer gender = NumUtils.getGenderByIdCard(idCard);

        //获取条形码地址
        String barCode = numService.createBarCode(carNum);
        //获取二维码地址
        String code = numService.createCode(carNum);

        //判断登录人是否有绑定默认就诊人
        Integer integer = patientMapper.queryIsDefaultByPatientUserId(patient.getPatientUserId());
        patient.setCardNum(carNum);
        patient.setBirthday(birthday);
        patient.setGender(gender);
        patient.setCardNumBarUrl(pathStr+barCode);
        patient.setCardNumUrl(pathStr+code);
        if(integer <= 0){
            //没有默认就诊人
            patient.setIsDefault(1);
            count = patientMapper.createPatient(patient);
            return count;
        }

        patient.setIsDefault(0);
        count = patientMapper.createPatient(patient);

        //添加
        return count;
    }

    /**
     * 根据token获取默认的就诊人信息
     */
    @Override
    public Result<Patient> getPatientByToken(Integer userId) {

        Patient patients = patientMapper.queryPatientByUser(userId);
        return ResultUtils.returnDataSuccess(patients);

        /*if(token == null || token.equals("")){
            return ResultUtils.returnResult(ResultEnum.FILE_NOT_LOGIN);
        }

        //token:PC-PHONE[加密]-USERID-CREATIONDATE-RONDEM[6 位]
        String[] array =token.split("-");
        //获取存入redis中的时间
        String timeStr = array[3];
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date timeToken = null;
        Date now = null;
        Long dff = null;

        try {
            timeToken = simpleDateFormat.parse(timeStr);//token中获取的时间
            //系统当前时间
            now = new Date();
            //系统当前时间减去从token中获取的时间差
            dff = now.getTime() - timeToken.getTime();
            //判断是否超过两小时
            if(dff > patientUserTokenTimeOut*1000){
                //时间已经超过两小时了
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        LoginPatientUserVo loginPatientUserVo = patientUserTokenService.getPatientUserByToken(token);
        LoginPatientUserDto loginPatientUserDto = new LoginPatientUserDto();

        String patientUserIdStr = array[2];
        Integer patientUserId = Integer.parseInt(patientUserIdStr);
        PatientUser patientUser = patientUserMapper.queryPatientByPatientUserId(patientUserId);
        loginPatientUserDto.setPhone(patientUser.getPhone());
        loginPatientUserDto.setPatientUserId(patientUserId);
        Patient patient = loginPatientUserVo.getPatient();
        if(patient == null){
            return ResultUtils.returnResult(ResultEnum.TOKEN_PATIENT_NULL);
        }
        if(loginPatientUserVo != null){
            //存在，则返回信息
            //并且一个半小时刷新token
            if(dff > patientUserTokenTimeOut *750){
                //删除之前的
                patientUserTokenService.deleteByToken(token);
                //生成新的用户信息报错到redis中
                patientUserService.getLoginPatientUserVo(patient.getPatientUserId(), loginPatientUserDto);
            }
            return ResultUtils.returnDataSuccess(patient);
        }
        //如果不存在，从MySql中查询
        //获取用户id

        patientUserService.getLoginPatientUserVo(patientUserId, loginPatientUserDto);
        return ResultUtils.returnDataSuccess(patient);*/
    }

    /**
     * 根据登录用户的ID查询就诊人数量
     */
    @Override
    public Integer queryCountByPatientUserId(Integer patientUserId) {
        return patientMapper.queryCountByPatientUserId(patientUserId);
    }


    /**
     * 判断身份证号是否重复
     */
    private Boolean checkIdCard(String idCard){
        Patient patient = patientMapper.queryPatientByIdCard(idCard);
        if(patient == null){
            return true;
        }
        return false;
    }
}
